/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.applet;

/**
 *
 * @author Mr.Sam
 */
import java.applet.*;
import java.util.*;
import fr.ove.applet.AppletLoadingChecked;
import fr.ove.applet.ListenableApplet;
import fr.ove.applet.events.AppletListener;
import fr.ove.applet.events.AppletEvent;

/**
* This is an implementation of the <CODE>AppletListener</CODE> interface.<BR>
* This applet takes as argument a parameter named <CODE>listened</CODE> which
* represents a list of <CODE>ListenableApplet</CODE> the instance will be listening to.<BR>
* Each element of the list is the name of the corresponding <CODE>ListenableApplet</CODE>.
* The elements of the list are separated by a coma (,).
* Ex: <PARAM NAME="listened" VALUE="{applet1, applet2}">
* <P>
* This class is an final class, so it can't be overloaded.
*
* @author © 1999 DIRAT Laurent
* @version 1.0  27/04/98
*/
public final class AnAppletListener extends AppletLoadingChecked implements AppletListener {
    int timeMax = 60;
    int timeout = 0;
    Thread timer;
    AppletLoadingChecked appletloading=new AppletLoadingChecked();
    /**
    * Initialyzes the instance by registering it to each <CODE>ListenableApplet</CODE>
    * in the list passed as argument.
    */
    public void init() {
        super.init();

        timer = new Thread() {
            public void run() {
                while (timeout < timeMax) {
                    try {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException ie) {
                    }
                    timeout++;
                }
            }
        };
		timer.start();

		Thread threadRegistering = new Thread() {
            public void run() {
                String listened = getParameter("listened");

        		if ((listened != null) && !listened.equals("")) {
            		Vector listOfListened = new Vector();
            		StringTokenizer tokenizer = new StringTokenizer(listened, "{, }");
            		while (tokenizer.hasMoreElements())
            		    listOfListened.addElement(tokenizer.nextToken());

            		Enumeration listeApplets;
                    Applet anApplet;
                    ListenableApplet aListenableApplet;
            		while ((listOfListened.size() != 0) && (timeout < timeMax)) {
                        listeApplets = getAppletContext().getApplets();

                        for (Enumeration e = listeApplets; e.hasMoreElements(); ) {
                            anApplet = (Applet) e.nextElement();

                            if (anApplet instanceof ListenableApplet) {
                                aListenableApplet = (ListenableApplet) anApplet;
                                String appletName = aListenableApplet.getName();

                                if (listOfListened.contains(appletName)) {
                                    int state = AppletLoadingChecked.LOADING;
                                    if (aListenableApplet instanceof AppletLoadingChecked) {
                                        state = ((AppletLoadingChecked) aListenableApplet).getState();
                                        while (state == AppletLoadingChecked.LOADING) {
                                            try {
                                                Thread.sleep(500);
                                            }
                                            catch (InterruptedException ie) {
                                            }

                                            state = ((AppletLoadingChecked) aListenableApplet).getState();
                                        }
                                    }

                                    if (state == AppletLoadingChecked.LOADED) {
                                        int instanceState =appletloading.getState();
                                        while (instanceState == AppletLoadingChecked.LOADING) {
                                            try {
                                                Thread.sleep(500);
                                            }
                                            catch (InterruptedException ie) {
                                            }

                                            instanceState = appletloading.getState();
                                        }
                                        aListenableApplet.addAppletListener(AnAppletListener.this);
                                        aListenableApplet.fireAppletEvent(new AppletEvent(aListenableApplet));
                                    }

                                    listOfListened.removeElement(appletName);
                                }
                            }
                        }
                    }

                    if (timeout == timeMax)
                        setState(AppletLoadingChecked.ERROR);
                    else
                        timer.stop();

                }

                stop();
            }
        };
        threadRegistering.start();
    }

    /**
    * Consumes (i.e. treats) the event received.
    * @param AppletEvent the event to consume.
    */
    public void consumeAppletEvent(AppletEvent appletEvent) {
        AppletListener appletListener = (AppletListener) getTheApplet();
        if (appletListener != null)
            appletListener.consumeAppletEvent(appletEvent);
    }
}

